﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 历史路径
    /// </summary>
    public class PathHistoty
    {
        /// <summary>
        /// 最大的历史路径总数
        /// </summary>
        private const int maxHistory = 10;
        
        /// <summary>
        /// 当前的历史索引
        /// </summary>
        private int currentHistory = 0;
        
        /// <summary>
        /// 历史路径集合
        /// </summary>
        private List<object> histories = new List<object>();
        
        /// <summary>
        /// 添加历史路径
        /// </summary>
        /// <param name="tag">要添加的路径路径</param>
        public void AddHistory(object tag)
        {
            if (histories.Count != 0)
            {
                if (histories[currentHistory].Equals(tag)) return;
            }
            if (currentHistory != histories.Count - 1)
            {
                int count = histories.Count;
                for (int i = count - 1; i > currentHistory; i--)
                {
                    try
                    {
                        histories.RemoveAt(i);
                    }
                    catch
                    {

                    }
                }
            }
            if (histories.Count == maxHistory)
            {
                histories.RemoveAt(0);
            }
            histories.Add(tag);
            int index = histories.IndexOf(tag);
            currentHistory = histories.Count - 1;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public object GetCurrentHistroy()
        {
            if (histories.Count == 0)
            {
                return null;
            }
            else
            {
                return histories[currentHistory];
            }
        }
        
        /// <summary>
        /// 后退
        /// </summary>
        /// <returns></returns>
        public object Back()
        {
            if (currentHistory == 0)
            {
                return null;
            }
            else
            {
                currentHistory--;
                return histories[currentHistory];
            }
        }
        
        /// <summary>
        /// 前进
        /// </summary>
        /// <returns></returns>
        public object Forward()
        {
            if (currentHistory < maxHistory && currentHistory < histories.Count - 1)
            {
                currentHistory++;
                return histories[currentHistory];
            }
            else
            {
                return null;
            }
        }
        
        /// <summary>
        /// 是否可以后退
        /// </summary>
        /// <returns></returns>
        public bool CanBack()
        {
            return currentHistory > 0;
        }
        
        /// <summary>
        /// 是否可以前进
        /// </summary>
        /// <returns></returns>
        public bool CanForward()
        {
            return currentHistory < histories.Count - 1;
        }
    }
}
